<?php

namespace Database\Seeders;

use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class NewsCommentSeeder extends Seeder
{
    // php artisan db:seed --class=NewsCommentSeeder

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // 清空数据表 后在添加
        $time = time();
        $toDb = DB::table('news_comment');
        $toDb->truncate();

        // 查询原来的数据
        $sql  = "select * from wp_comments;";
        $coms = DB::select($sql);
        $coms = json_decode(json_encode($coms), true);

        // 整理数据
        $all = [];
        foreach($coms as $_com){
            $all[] = $this->makeRow($_com, $time);
        }

        // 执行
        $toDb->insert($all);
    }


    // 生成数据
    protected function makeRow($_com, $time)
    {

        $_row = [
            'id'               => $_com['comment_ID'],
            'uid'              => $_com['user_id'],
            'news_id'          => $_com['comment_post_ID'],
            'pid'              => $_com['comment_parent'],
            'author'           => $_com['comment_author'],
            'author_ip'        => $_com['comment_author_IP'],
            'en_content'       => $_com['comment_content'],
            'cn_content'       => '',
            'tc_content'       => '',
            'comment_karma'    => $_com['comment_karma'],
            'comment_approved' => $_com['comment_approved'],
            'comment_type'     => $_com['comment_type'],
            'ctime'            => strtotime($_com['comment_date']),
            'mtime'            => strtotime($_com['comment_date_gmt']),
        ];

        return $_row;
    }

}
